package com.jht.bletool2java.characteristic.deviceinfromations;

import android.bluetooth.BluetoothGattCharacteristic;
import android.util.Log;

import androidx.annotation.Keep;

import com.jht.bletool2java.characteristic.TranslateData;
import com.jht.bletool2java.util.ByteUtil;

import top.codestudy.annotation_uuid.MyUUID;

@Keep
@MyUUID(uuid = "00002a23-0000-1000-8000-00805f9b34fb")
public class SystemID implements TranslateData {
    private static final String TAG = "SystemID";
    private String systemID = "";
    private String manufacturerIdentifier = "";
    private String organizationallyUniqueIdentifier = "";
    private byte[] value;
    private boolean isInvalidData = false;
    public SystemID() {
    }

    public SystemID(BluetoothGattCharacteristic characteristic) {
        if (characteristic == null) {
            Log.w(TAG, "SystemID: characteristic is null!");
            isInvalidData = true;
            return;
        }
        value = characteristic.getValue();
        isInvalidData = (value == null || value.length < 8);
        if (!isInvalidData){
            long mManufacturerIdentifier = ByteUtil.byte5ToIntR(value, 0);
            int mOrganizationallyUniqueIdentifier = ByteUtil.byte3ToIntR(value, 5);
            Log.i(TAG, "SystemID: mManufacturerIdentifier ==> " + mManufacturerIdentifier + " ; mOrganizationallyUniqueIdentifier ==> " + mOrganizationallyUniqueIdentifier);
            manufacturerIdentifier += "0x";
            String mManufacturerIdentifierStr = Long.toHexString(mManufacturerIdentifier);
            if (mManufacturerIdentifierStr.length() <= 10) {
                for (int i = 0; i < 10 - mManufacturerIdentifierStr.length(); i++) {
                    manufacturerIdentifier += "0";
                }
            }
            manufacturerIdentifier += mManufacturerIdentifierStr;

            organizationallyUniqueIdentifier += "0x";
            String organizationallyUniqueIdentifierStr = Integer.toHexString(mOrganizationallyUniqueIdentifier);
            if (organizationallyUniqueIdentifierStr.length() <= 10) {
                for (int i = 0; i < 6 - organizationallyUniqueIdentifierStr.length(); i++) {
                    organizationallyUniqueIdentifier += "0";
                }
            }
            organizationallyUniqueIdentifier += organizationallyUniqueIdentifierStr;
        }
    }

    @Override
    public String convert2String() {
        return "Manufacturer Identifier: " + manufacturerIdentifier + "\n ; organizationallyUniqueIdentifier: " + organizationallyUniqueIdentifier + "\n";
    }

    @Override
    public String merge(TranslateData translateData) {
        return null;
    }

    @Override
    public boolean hasMoreData() {
        return false;
    }

    @Override
    public byte[] getData() {
        return value;
    }

    @Override
    public boolean isInvalidData() {
        return isInvalidData;
    }

    @Override
    public void parseData(BluetoothGattCharacteristic characteristic, byte[] value) {
        isInvalidData = (value == null || value.length < 8);
        if (isInvalidData) {
            //数据非法
            this.value = value;
            manufacturerIdentifier = "";
            organizationallyUniqueIdentifier = "";
            Log.w(TAG, "parseData: value == null || value.length != 8");
        }else{
            this.value = value;
            long mManufacturerIdentifier = ByteUtil.byte5ToIntR(value, 0);
            int mOrganizationallyUniqueIdentifier = ByteUtil.byte3ToIntR(value, 5);
            Log.i(TAG, "SystemID: mManufacturerIdentifier ==> " + mManufacturerIdentifier + " ; mOrganizationallyUniqueIdentifier ==> " + mOrganizationallyUniqueIdentifier);
            manufacturerIdentifier = "0x";
            String mManufacturerIdentifierStr = Long.toHexString(mManufacturerIdentifier);
            if (mManufacturerIdentifierStr.length() <= 10) {
                for (int i = 0; i < 10 - mManufacturerIdentifierStr.length(); i++) {
                    manufacturerIdentifier += "0";
                }
            }
            manufacturerIdentifier += mManufacturerIdentifierStr;

            organizationallyUniqueIdentifier = "0x";
            String organizationallyUniqueIdentifierStr = Integer.toHexString(mOrganizationallyUniqueIdentifier);

            for (int i = 0; i < 6 - organizationallyUniqueIdentifierStr.length(); i++) {
                organizationallyUniqueIdentifier += "0";
            }

            organizationallyUniqueIdentifier += organizationallyUniqueIdentifierStr;
        }

    }


}
